package com.android.lovepick.util;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;

public class DialogHelper extends DialogFragment
{
	private final String DIALOGHELPER_TAG = "DialogHelper";
	private final static String DIALOG_ID = "dialogId";
	private final static String TITLE = "title";
	private final static String MSG = "msg";
	private final static String ITEMS = "items";
	private static DialogInterface.OnClickListener mListener;

	private String captionPositive = "Pos";
	private String captionNegative = "Negative";
	private String captionNeutral = "Neutral";

	public static DialogHelper newInstance(int dialogId, String title,
			String msg, DialogInterface.OnClickListener dialogClickListener)
	{
		DialogHelper fragment = new DialogHelper();
		mListener = dialogClickListener;

		Bundle bundle = new Bundle();
		bundle.putInt(DIALOG_ID, dialogId);
		bundle.putString(TITLE, title);
		bundle.putString(MSG, msg);
		bundle.putStringArray(ITEMS, new String[] { "" });
		fragment.setArguments(bundle);
		return fragment;
	}

	public static DialogHelper newInstance(int dialogId, String title,
			String[] items, DialogInterface.OnClickListener dialogClickListener)
	{
		DialogHelper fragment = new DialogHelper();
		mListener = dialogClickListener;
		Bundle bundle = new Bundle();
		bundle.putInt(DIALOG_ID, dialogId);
		bundle.putString(TITLE, title);
		bundle.putString(MSG, "");
		bundle.putStringArray(ITEMS, items);
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public Dialog onCreateDialog(Bundle bundle)
	{
		Dialog dialog = null;
		int dialogId = getArguments().getInt(DIALOG_ID);
		Log.i(DIALOGHELPER_TAG, "dialogId: " + String.valueOf(dialogId));
		String title = getArguments().getString(TITLE);
		Log.i(DIALOGHELPER_TAG, "title: " + title);
		String msg = getArguments().getString(MSG);
		Log.i(DIALOGHELPER_TAG, "msg: " + msg);
		String[] items = getArguments().getStringArray(ITEMS);
		Log.i(DIALOGHELPER_TAG, "items: " + items.toString());

		switch (dialogId)
		{
			case Constant.DIALOG_POS:
				dialog = createDialogPos(title, msg);
				break;

			case Constant.DIALOG_POS_NEG:
				dialog = createDialogPosNeg(title, msg);
				break;

			case Constant.DIALOG_POS_NEG_NEU:
				dialog = createDialogPosNegNeu(title, msg);
				break;

			case Constant.DIALOG_ITEMS:
				createDialogItems(title, items);
				break;

			default:
				break;
		}

		return dialog;
	}

	private AlertDialog.Builder createDialogBuilder(String title, String msg)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle(title);
		builder.setMessage(msg);
		return builder;
	}

	private AlertDialog.Builder createDialogBuilder(String title, String[] items)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle(title);
		builder.setItems(items, mListener);
		return builder;
	}

	/**
	 * 带有标题的信息提示对话框, 该对话框只包含一个按钮.
	 * 
	 * @param title
	 *            消息标题
	 * @param msg
	 *            消息内容
	 * @return 创建的dialog
	 */
	private Dialog createDialogPos(String title, String msg)
	{
		AlertDialog.Builder builder = createDialogBuilder(title, msg);
		builder.setPositiveButton(captionPositive, mListener);
		return builder.create();
	}

	/**
	 * 带有标题的信息提示对话框, 该对话框包含两个按钮.
	 * 
	 * @param title
	 *            消息标题
	 * @param msg
	 *            消息内容
	 * @return 创建的dialog
	 */
	private Dialog createDialogPosNeg(String title, String msg)
	{
		AlertDialog.Builder builder = createDialogBuilder(title, msg);
		builder.setPositiveButton(captionPositive, mListener);
		builder.setNegativeButton(captionNegative, mListener);
		return builder.create();
	}

	/**
	 * 带有标题的信息提示对话框, 该对话框包含三个按钮.
	 * 
	 * @param title
	 *            消息标题
	 * @param msg
	 *            消息内容
	 * @return 创建的dialog
	 */
	private Dialog createDialogPosNegNeu(String title, String msg)
	{
		AlertDialog.Builder builder = createDialogBuilder(title, msg);
		builder.setPositiveButton(captionPositive, mListener);
		builder.setNegativeButton(captionNegative, mListener);
		builder.setNeutralButton(captionNeutral, mListener);
		return builder.create();
	}

	private Dialog createDialogItems(String title, String[] items)
	{
		AlertDialog.Builder builder = createDialogBuilder(title, items);
		return builder.create();
	}

	/**
	 * @param captionPositive
	 *            the captionPositive to set
	 */
	public void setCaptionPositive(String captionPositive)
	{
		this.captionPositive = captionPositive;
	}

	/**
	 * @param captionNegative
	 *            the captionNegative to set
	 */
	public void setCaptionNegative(String captionNegative)
	{
		this.captionNegative = captionNegative;
	}

	/**
	 * @param captionNeutral
	 *            the captionNeutral to set
	 */
	public void setCaptionNeutral(String captionNeutral)
	{
		this.captionNeutral = captionNeutral;
	}

}
